@import '@material/theme/functions.import';
@import '@material/tab-indicator/mixins.import';
@import '@material/tab/mixins.import';
@import '@material/tab/variables.import';
@import '@material/tab-bar/mixins.import';
@import '../mdc-helpers/mdc-helpers';

@mixin mat-mdc-tabs-color($config-or-theme) {
  $config: mat-get-color-config($config-or-theme);
  // Save original values of MDC global variables. We need to save these so we can restore the
  // variables to their original values and prevent unintended side effects from using this mixin.
  $orig-mdc-tab-text-label-color-active: $mdc-tab-text-label-color-active;
  $orig-mdc-tab-icon-color-active: $mdc-tab-icon-color-active;
  $orig-mdc-tab-text-label-color-default: $mdc-tab-text-label-color-default;

  @include mat-using-mdc-theme($config) {
    // This value is the same as MDC's default, but MDC defines it once inside
    // a variables file which means that we can't override it with our own palette.
    $mdc-tab-text-label-color-default:
        rgba(mdc-theme-prop-value(on-surface), $mdc-tab-text-label-opacity) !global;

    @include _mat-mdc-tabs-palette-styles($mdc-tab-text-label-color-active);

    .mat-mdc-tab-group, .mat-mdc-tab-nav-bar {
      &.mat-accent {
        $mdc-tab-text-label-color-active: secondary !global;
        $mdc-tab-icon-color-active: secondary !global;
        @include _mat-mdc-tabs-palette-styles($mdc-tab-text-label-color-active);
      }

      &.mat-warn {
        $mdc-tab-text-label-color-active: error !global;
        $mdc-tab-icon-color-active: error !global;
        @include _mat-mdc-tabs-palette-styles($mdc-tab-text-label-color-active);
      }
    }

    .mat-mdc-tab-group {
      &.mat-background-primary {
        @include _mat-mdc-tabs-background(primary, on-primary);
      }

      &.mat-background-accent {
        @include _mat-mdc-tabs-background(secondary, on-secondary);
      }

      &.mat-background-warn {
        @include _mat-mdc-tabs-background(error, on-error);
      }
    }

    .mat-mdc-tab-header-pagination-chevron {
      @include mdc-theme-prop(border-color, on-surface);
    }
  }

  // Restore original values of MDC global variables.
  $mdc-tab-text-label-color-active: $orig-mdc-tab-text-label-color-active !global;
  $mdc-tab-icon-color-active: $orig-mdc-tab-icon-color-active !global;
  $mdc-tab-text-label-color-default: $orig-mdc-tab-text-label-color-default !global;
}

@mixin _mat-mdc-tabs-background($background-color, $foreground-color) {
  .mat-mdc-tab-header, .mat-mdc-tab-links, .mat-mdc-tab-header-pagination {
    // Set background color for the tab group
    @include mdc-theme-prop(background-color, $background-color);
  }

  // Set labels to contrast against background
  .mdc-tab__text-label, .mat-mdc-tab-link {
    @include mdc-theme-prop(color, $foreground-color);
  }

  .mat-ripple-element, .mdc-tab__ripple::before {
    @include mdc-theme-prop(background-color, $foreground-color);
  }

  .mdc-tab-indicator__content--underline, .mat-mdc-tab-header-pagination-chevron {
    @include mdc-theme-prop(border-color, $foreground-color);
  }
}

@mixin _mat-mdc-tabs-palette-styles($color) {
  @include mdc-tab-without-ripple($query: $mat-theme-styles-query);
  @include mdc-tab-indicator-underline-color($color, $query: $mat-theme-styles-query);
  @include mdc-tab-indicator-icon-color($color, $query: $mat-theme-styles-query);

  .mdc-tab__ripple::before,
  .mat-mdc-tab .mat-ripple-element,
  .mat-mdc-tab-header-pagination .mat-ripple-element,
  .mat-mdc-tab-link .mat-ripple-element {
    @include mdc-theme-prop(background-color, $color);
  }
}

@mixin mat-mdc-tabs-typography($config-or-theme) {
  $config: mat-get-typography-config($config-or-theme);
  @include mat-using-mdc-typography($config) {
    @include mdc-tab-without-ripple($query: $mat-typography-styles-query);
    @include mdc-tab-indicator-core-styles($query: $mat-typography-styles-query);
  }
}

@mixin mat-mdc-tabs-density($config-or-theme) {
  $density-scale: mat-get-density-config($config-or-theme);
  .mat-mdc-tab-header {
    @include mdc-tab-bar-density($density-scale, $query: $mat-base-styles-query);
  }
}

@mixin mat-mdc-tabs-theme($theme-or-color-config) {
  $theme: mat-private-legacy-get-theme($theme-or-color-config);
  @include mat-private-check-duplicate-theme-styles($theme, 'mat-mdc-tabs') {
    $color: mat-get-color-config($theme);
    $density: mat-get-density-config($theme);
    $typography: mat-get-typography-config($theme);

    @if $color != null {
      @include mat-mdc-tabs-color($color);
    }
    @if $density != null {
      @include mat-mdc-tabs-density($density);
    }
    @if $typography != null {
      @include mat-mdc-tabs-typography($typography);
    }
  }
}

